﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

namespace TurnamentOrganizer
{
    public partial class MainForm : Form
    {
        readonly Tournament _main;
        
        public MainForm(Tournament main)
        {
            InitializeComponent();
            _main = main;

            dataGridView1.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn nameCol = new DataGridViewTextBoxColumn();
            nameCol.DataPropertyName = "Name";
            nameCol.HeaderText = "Navn";

            dataGridView1.Columns.Add(nameCol);
            
            DataGridViewTextBoxColumn paidCol = new DataGridViewTextBoxColumn();
            paidCol.DataPropertyName = "hasPaid";
            paidCol.HeaderText = "Har Betalt?";

            dataGridView1.Columns.Add(paidCol);

            DataGridViewTextBoxColumn winsCol = new DataGridViewTextBoxColumn();
            winsCol.DataPropertyName = "Wins";
            winsCol.HeaderText = "Vundne";

            dataGridView1.Columns.Add(winsCol);

            DataGridViewTextBoxColumn lossCol = new DataGridViewTextBoxColumn();
            lossCol.DataPropertyName = "Losses";
            lossCol.HeaderText = "Tabte";

            dataGridView1.Columns.Add(lossCol);

            DataGridViewTextBoxColumn CPCol = new DataGridViewTextBoxColumn();
            CPCol.DataPropertyName = "ControlPoints";
            CPCol.HeaderText = "CP";

            dataGridView1.Columns.Add(CPCol);            

            DataGridViewTextBoxColumn SosCol = new DataGridViewTextBoxColumn();
            SosCol.DataPropertyName = "Sos";
            SosCol.HeaderText = "Sos";

            dataGridView1.Columns.Add(SosCol);

            DataGridViewTextBoxColumn homeCol = new DataGridViewTextBoxColumn();
            homeCol.DataPropertyName = "HomeLocation";
            homeCol.HeaderText = "Home";

            dataGridView1.Columns.Add(homeCol);

            //var source = new BindingSource();
            //source.DataSource = main.playerList;
            //dataGridView1.DataSource = source;

            dataGridView1.BindingContext = new BindingContext();
            dataGridView1.DataSource = main.playerList;            
        }

        private void buttonAddPlayers_Click(object sender, EventArgs e)
        {
            _main.AddPlayers();
        }        

        private void buttonPayment_Click(object sender, EventArgs e)
        {
            var paymentForm = new PaymentForm(_main);
            paymentForm.Show();
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Add test locations
            _main.Locations.Add("Roskilde");
            _main.Locations.Add("København");
            _main.Locations.Add("Århus");
            _main.Locations.Add("Odense");

            //Add test players
            var player1 = new Player("Peter") { Wins = 3, ControlPoints = 5, HasPaid = true, HomeLocation = "Roskilde"};
            var player2 = new Player("Josefine") { Wins = 3, ControlPoints = 3, HasPaid = false, HomeLocation = "København" };
            var player3 = new Player("Søren K") { Wins = 1, ControlPoints = 1, HasPaid = true, HomeLocation = "Odense" };
            var player4 = new Player("Peter K") { Wins = 4, ControlPoints = 2, HasPaid = false, HomeLocation = "København" };
            var player5 = new Player("Gil") { Wins = 2, ControlPoints = 5, HasPaid = true, HomeLocation = "Århus" };
            var player6 = new Player("Benjamin") { Wins = 0, ControlPoints = 3, HasPaid = false, HomeLocation = "Roskilde" };


            if (_main != null)
            {
                _main.playerList.Add(player1);
                _main.playerList.Add(player2);
                _main.playerList.Add(player3);
                _main.playerList.Add(player4);
                _main.playerList.Add(player5);
                _main.playerList.Add(player6);
            }

            var playerItemList = new List<ListViewItem>();

            foreach (Player player in _main.playerList)
            {
                var currentItem = new ListViewItem(player.Name);
                currentItem.SubItems.Add(player.Wins.ToString(CultureInfo.InvariantCulture));
                currentItem.SubItems.Add(player.ControlPoints.ToString(CultureInfo.InvariantCulture));

                playerItemList.Add(currentItem);                
            }
        }

        private void buttonNextRound_Click(object sender, EventArgs e)
        {
            //Start the next round

            //Show the list of players which will be battling each other
            NextRoundForm nextRoundForm = new NextRoundForm(_main);
            nextRoundForm.Show();
            
        }

        private void opsætningToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LocationForm locationForm = new LocationForm(_main);
            locationForm.Show();
        }

        private void buttonResult_Click(object sender, EventArgs e)
        {
            var addResultForm = new ResultsForm(_main);
            addResultForm.Show();
        }

        private void indsætBlankTestDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Add test locations
            _main.Locations.Add("Roskilde");
            _main.Locations.Add("København");
            _main.Locations.Add("Århus");
            _main.Locations.Add("Odense");

            //Add test players
            var player1 = new Player("Peter") { HasPaid = true, HomeLocation = "Roskilde" };
            var player2 = new Player("Josefine") { HasPaid = false, HomeLocation = "København" };
            var player3 = new Player("Søren K") { HasPaid = true, HomeLocation = "Odense" };
            var player4 = new Player("Peter K") { HasPaid = false, HomeLocation = "København" };
            var player5 = new Player("Gil") { HasPaid = true, HomeLocation = "Århus" };
            var player6 = new Player("Benjamin") { HasPaid = false, HomeLocation = "Roskilde" };


            if (_main != null)
            {
                _main.playerList.Add(player1);
                _main.playerList.Add(player2);
                _main.playerList.Add(player3);
                _main.playerList.Add(player4);
                _main.playerList.Add(player5);
                _main.playerList.Add(player6);
            }

            var playerItemList = new List<ListViewItem>();

            foreach (Player player in _main.playerList)
            {
                var currentItem = new ListViewItem(player.Name);
                currentItem.SubItems.Add(player.Wins.ToString(CultureInfo.InvariantCulture));
                currentItem.SubItems.Add(player.ControlPoints.ToString(CultureInfo.InvariantCulture));

                playerItemList.Add(currentItem);
            }
        }

        private void buttonCalcSOS_Click(object sender, EventArgs e)
        {
            List<Player> resultList = (from players in _main.playerList.ToList()
                                       orderby players.ControlPoints descending
                                       orderby players.Wins descending
                                       select players).ToList();

            foreach (var player in _main.playerList)
            {
                player.CalculateSOS(resultList);
            }
        }
    }
}
